# encoding: utf-8

import re
import urllib2
import base64
import time
import json

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"}

pattern = re.compile("<span class=\"img-hash\">([\w=]+)</span>")

def fetch():
	url = "http://jandan.net/ooxx"

	while url:
		req = urllib2.Request(url, None, headers)
		url = None
		
		conn = urllib2.urlopen(req)
		text = conn.read()
		conn.close()

		for obj in pattern.finditer(text):
			yield "http:"+base64.b64decode(obj.group(1))

		for obj in re.finditer('href="([^"]+)" class="previous-comment-page"', text):
			url = "http:"+obj.group(1)
			break

ding = "https://oapi.dingtalk.com/robot/send?access_token=26fad7b55319019689201c0bf50022a89542b1aa95d81cceca9996b2958771b0"

count = 0
for url in fetch():
	if count < 20:
		count += 1
		continue

	body = {
		"msgtype": "markdown",
		"markdown": {
			"title": "濡瑰瓙鍥?鉁库棔鈥库棔鉁?",
			"text": "![濡瑰瓙](%s)" % url,
		}
	}

	req = urllib2.Request(ding, json.dumps(body), {"Content-Type": "application/json"})
	resp = urllib2.urlopen(req)
	print(resp.read())
	resp.close()
	time.sleep(30)

